<template>
  <div class="liquidGlass-wrapper dock">
    <div class="liquidGlass-effect"></div>
    <div class="liquidGlass-tint"></div>
    <div class="liquidGlass-shine"></div>
    <div class="liquidGlass-text">
      <div class="nav-left">
        <img src="../assets/logo.jpg" class="logo" @click="router.push('/index')" />
        <el-input
          type="text"
          :placeholder="searchHot.title || '搜索感兴趣的帖子'"
          class="search-input"
          v-model="searchQuery"
          @keyup.enter="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </div>
      <div class="nav-center">
        <el-menu
          mode="horizontal"
          background-color="transparent"
          border-bottom="none"
          text-color="#333"
          active-text-color="#0866ff"
          @select="goto"
          :default-active="active"
        >
          <el-menu-item index="index">
            <el-icon size="30" class="header-icon">
              <HomeFilled />
            </el-icon>
          </el-menu-item>
          <el-menu-item index="userinfo">
            <el-icon size="30" class="header-icon">
              <UserFilled />
            </el-icon>
          </el-menu-item>
          <el-menu-item index="contact">
            <el-icon size="30" class="header-icon">
              <Promotion />
            </el-icon>
          </el-menu-item>
        </el-menu>
      </div>
      <div class="nav-right">
        <el-dropdown style="float: right">
          <span class="el-dropdown-link">
            <img
              :src="userinfo.avatar || '/assets/avatar/default.jpg'"
              class="user-avatar"
            />
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="ToUserinfo()">个人资料</el-dropdown-item>
              <el-dropdown-item @click="exit">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
  <div style="height: 60px"></div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { HomeFilled, UserFilled, Promotion } from "@element-plus/icons-vue";
import userStore from "../store/index";
import { useRouter, useRoute } from "vue-router";
import api from "../utils/index.js";
import { Search } from "@element-plus/icons-vue";

const router = useRouter();
const userStoreInfo = userStore();
const user = userStoreInfo.getUser();
const articles = ref([]);
const userinfo = ref({});
const searchQuery = ref("");
const searchHot = ref({});
const currentIndex = ref(0);
const route = useRoute();
const active = route.name;

// 获取用户信息和联系人
const fetchUserInfo = async () => {
  try {
    const response = await api.get(`/users?id=${user.id}`);
    userinfo.value = response.data[0];
  } catch (error) {
    console.error("获取用户信息失败:", error);
  }
};

// 获取文章
const fetchArticles = async () => {
  try {
    const response = await api.get("/articles?_limit=10");
    articles.value = response.data.map((article) => ({
      id: article.id,
      title: article.content.substring(0, 12) + "...",
    }));
  } catch (error) {
    console.error("获取文章失败:", error);
  }
};

setInterval(() => {
  if (articles.value.length > 0) {
    searchHot.value = articles.value[currentIndex.value];
    currentIndex.value = (currentIndex.value + 1) % articles.value.length;
  }
}, 2500);

// 处理搜索
const handleSearch = (event) => {
  const keyword = event.target.value.trim();
  if (keyword) {
    router.push({ path: "/search", query: { q: keyword } });
  } else {
    router.push({ path: "/articleinfo", query: { id: searchHot.value.id } });
  }
};

// 跳转到指定页面
const goto = (index) => {
  router.push(`/${index}`);
};
// 退出登录
const exit = () => {
  userStoreInfo.clearUser();
  router.push("/login");
};

// 跳转个人资料
const ToUserinfo = () => {
  router.push("/userinfo");
};

onMounted(async () => {
  await fetchUserInfo();
  fetchArticles();
});
</script>

<style>
.header {
  background-color: #fff;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* 确保 header 元素在顶层 */
  z-index: 1;
  position: relative;
  /* 添加过渡动画 */
  opacity: 1;
  backdrop-filter: blur(10px);
  background-color: rgb(255 255 255 / 23%);
  transition: opacity 0.3s ease;
}

/* 固定定位样式 */
.fixed-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}

.nav-left {
  width: 280px;
  display: flex;
  align-items: center;
}

.logo {
  height: 40px;
  margin-right: 10px;
  border-radius: 40px;
}

.search-input {
  width: 280px;
  height: 40px;
  border: 1px solid #e0e0e0;
  border-radius: 20px;
  text-indent: 12px;
  background-color: #f5f5f5;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s ease;
}

.search-input:hover {
  border-color: #0866ff;
}
.search-input:focus {
  border-color: #0866ff;
}
.nav-center {
  width: 20%;
  padding-left: 5%;
}
.el-menu {
  border-bottom: none !important; /* 去掉底部边框 */
}
.header-icon {
  color: #333; /* 图标颜色，按需改 */
}
.nav-right {
  width: 280px;
}

.user-avatar {
  height: 40px;
  width: 40px;
  border-radius: 50%;
}
.header-icon {
  margin-right: 0 !important;
}

.liquidGlass-wrapper {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 999;
  display: flex;
  font-weight: 600;
  overflow: hidden;

  color: black;

  box-shadow: 0 6px 6px rgba(0, 0, 0, 0.2), 0 0 20px rgba(0, 0, 0, 0.1);

  transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 2.2);
}

.liquidGlass-effect {
  position: absolute;
  z-index: 0;
  inset: 0;

  backdrop-filter: blur(3px);
  filter: url(#glass-distortion);
  overflow: hidden;
  isolation: isolate;
}

.liquidGlass-tint {
  z-index: 1;
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.25);
}

.liquidGlass-shine {
  position: absolute;
  inset: 0;
  z-index: 2;

  overflow: hidden;

  box-shadow: inset 2px 2px 1px 0 rgba(255, 255, 255, 0.5),
    inset -1px -1px 1px 1px rgba(255, 255, 255, 0.5);
}

.liquidGlass-text {
  padding: 0 20px;
  width: 100%;
  z-index: 3;
  font-size: 2rem;
  justify-content: space-between;
  align-items: center;
  display: flex;
  color: black;
}
</style>
